package com.tju.resource.oauth.controller.sys;

import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.common.ServiceException;
import com.tju.resource.mapper.SysRolePermissionMapper;
import com.tju.resource.model.SysRole;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.business.sys.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.rmi.ServerException;
import java.util.Map;

import static com.tju.resource.business.sys.SysRoleService.Response.*;

/**
 * 角色CRUD
 *
 * @Author CLS
 * @Date 2021/12/31 14:13
 */
@RestController
public class SysRoleController {

    private static final String prefix = "role";

    @Autowired
    public SysRoleService service;

    @Autowired
    public HttpServletResponse response;

    @Autowired
    SysRolePermissionMapper rpMapper;

    @PostMapping(prefix + "/getAll")
    @PreAuthorize("hasAuthority('sys:manage:role:list')")
    public Result<Object> getCharacterAll(@RequestBody ClientParameters parameter) {
        response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        return service.getCharacterAll(parameter);
    }

    @PostMapping(prefix + "/add")
    @PreAuthorize("hasAuthority('sys:manage:role:add')")
    public Result<Object> addCharacter(@RequestBody Map<String, String> params) {
        try {
            if (null != params && !StringUtils.equals(params.get("name"), "")) {
                if (!service.hasRoleName(params.get("name"))) {
                    service.addCharacter(params);
                    return ResultUtil.success(ResultEnum.SUCCESS);
                } else {
                    throw new ServerException(DUP_NA);
                }
            } else {
                throw new ServiceException(NONE_NA);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @PostMapping(prefix + "/del")
    @PreAuthorize("hasAuthority('sys:manage:role:delete')")
    public Result<Object> delCharacter(@RequestBody Map<String, String> params) {
        try {
            if (null != params && null != params.get("id")) {
                if (null != service.getCharacter(Long.parseLong(params.get("id")))) {
                    service.delCharacter(params);
                    return ResultUtil.success(ResultEnum.SUCCESS);
                } else {
                    throw new ServiceException(NONE_SR);
                }
            } else {
                throw new ServiceException(NONE_SR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @PostMapping(prefix + "/upd")
    @PreAuthorize("hasAuthority('sys:manage:role:edit')")
    public Result<Object> updCharacter(@RequestBody Map<String, String> params) {
        try {
            SysRole character = service.getCharacter(Long.parseLong(params.get("id")));
            Result<Object> success = ResultUtil.success(ResultEnum.SUCCESS);

            if (!StringUtils.equals(character.getName(), params.get("name"))) {
                if (!service.hasRoleName(params.get("name"))) {
                    service.updCharacter(params);
                    return success;
                } else {
                    throw new ServiceException("角色名称不可重复!");
                }
            } else {
                if (!StringUtils.equals(params.get("name"), "")) {
                    service.updCharacter(params);
                    return success;
                } else {
                    throw new ServiceException(NONE_NA);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

}
